;(function($){
	$.fn.spa_guestAccountPay = function(opts){
		var el = this;
		var opt = {
			operation:"",//付款方式
			xhs:"",
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//关闭
			el.find("#close-dialog,#modalBtnCancel").unbind("click").click(function(){
				el.modal("hide");
				el.empty();
				param.closeBack();
			});
			
			//输入钱数
    		el.find("#pay_btn").bind("click",function(){
    			//计算器
				var status = false;
				el.find("#modal-dialog").hide();
				if(!$.isFunction($().wechatAlipayPay)){
					$.ajaxSetup({ async: false, cache: false });
		            $.getScript("js/openSpa/jquery.plugin.digitalCalculator.js");
		            $.ajaxSetup({ async: true });
				}
				el.find("#mykeyboard").digitalCalculator({
					number:el.find("#pay").val()?el.find("#pay").val():1,//数量
					status:status?status:false,
					title:param.title?param.title:"",
					//初始化数据返回回调
					dataBack:function(number){
						el.find("#modal-dialog").show();
						el.find("#mykeyboard").hide();
 						el.find("#pay").val(number);
 						if(number > parseInt(param.price)&&param.totalPrice!=number){
 							el.find("#change").val(checkTwoPoint(number * 1 - param.price * 1));
 						}
					},
					returnBack :function(){
						el.find("#modal-dialog").show();
						el.find("#mykeyboard").hide();
					}
				});
    		});
    		
    		//选房间
    		el.find("#guest_barcode_search").bind("click",function(){
    			var gname = "";
    			if(!!el.find("#guest_barcode").val()){
    				gname = el.find("#guest_barcode").val();
    			}else{
    				gname = el.find("#guest_user").val();
    			}
    			el.find("#guest_barcode_search").modalFrame("destroy");
    			el.find("#guest_barcode_search").modalFrame("init",function(modalEle){   
					$("#"+modalEle).modal("show"); 
					if(!$.isFunction($().ho_guest_gledger_search)){
						$.ajaxSetup({ async: false, cache: false });
			            $.getScript("js/cashier_mana/jquery.service.ho_guest_gledger_search.js");
			            $.ajaxSetup({ async: true });
					}
					$("#"+modalEle).ho_guest_gledger_search({
	    				gname:gname,
	    				multiSelect :false,
	    				arg:"客账",
	    				dataBack : function(data){
//	    					console.log(data);
	    					el.find("#guest_user").val(data.gname);
	    					el.find("#guest_barcode").val(data.room);
	    					param.gno = data.gno;
	    					$("#"+modalEle).modal("hide");
	    					$("#"+modalEle).empty();
	    				},
	    				closeBack:function(){
	    					$("#"+modalEle).modal("hide");
	    					$("#"+modalEle).empty();
	    				}
	    			});
				});
    		});
    		
    		//选人
    		el.find("#guest_user_search").bind("click",function(){  	
    			var gname = "";
    			if(!!el.find("#guest_barcode").val()){
    				gname = el.find("#guest_barcode").val();
    			}else{
    				gname = el.find("#guest_user").val();
    			}
    			el.find("#guest_user_search").modalFrame("destroy");
    			el.find("#guest_user_search").modalFrame("init",function(modalEle){   
					$("#"+modalEle).modal("show"); 
					$("#"+modalEle).ho_guest_gledger_search({
	    				gname :gname,
	    				multiSelect : false,
	    				arg:"客账",
	    				dataBack : function(data){
	    					el.find("#guest_user").val(data.gname);
	    					el.find("#guest_barcode").val(data.room);
	    					param.gno = data.gno;
	    					$("#"+modalEle).modal("hide");
	    					$("#"+modalEle).empty();
	    				},
	    				closeBack:function(){
	    					$("#"+modalEle).modal("hide");
	    					$("#"+modalEle).empty();
	    				}
	    			});
				});
    		});
    		
    		//确定
    		el.find("#modalBtnSave").bind("click",function(){
    			t.wait();
    			var type = {"morepayment":"NO"};
    			var all = true;
    			if(parseFloat(el.find("#pay").val()) >= parseFloat(param.price)){//结清
    				type = {"morepayment":"NO"};
    				all = true; //判断是否结清，结清打印账单
    			}else{
    				type = {"morepayment":"YES"};  
    				all = false;//判断是否结清，结清打印账单
    			}
//    			var type = {"balancezero":"YES"};(不在贷方的时候才用这个参数：接口，例如信用卡，支付宝) TODO
//    			var all = false;
    			if(!$.isFunction($().spa_posorderAlonePay)){
					$.ajaxSetup({ async: false, cache: false });
		            $.getScript("js/cashier_mana/spa_posorderAlonePay.js");
		            $.ajaxSetup({ async: true });
				}
    			var json = {};
    			json.gno = param.gno?param.gno:"",//gno
    			json.post_room = param.no?param.no:"",//no号
				json.dcode = param.bh?param.bh:"",//科目号
				json.text = "",//
				json.text1 = el.find("#guest_remark").val()?el.find("#guest_remark").val():"",//备注
				json.price = el.find("#pay").val()?el.find("#pay").val():"",//金额
				json.quantity = "1",//数量
				json.debit = el.find("#pay").val()?el.find("#pay").val():"";//金额
				
				var pay = el.find("#pay").val()?el.find("#pay").val():""
    			var isTogather = false
    			if(parseFloat(param.totalPrice)<=parseFloat(checkTwoPoint(pay))&&param.nos.length>0){
    				isTogather = true;
    			}	
				
    			$("").spa_posorderAlonePay({
					no:param.no?param.no:"",//no号
					bh:param.bh?param.bh:"",//科目号
					mc:param.mc?param.mc:"",//mc
					type:type?type:"",//支付类型
					price:el.find("#pay").val()?el.find("#pay").val():"",//金额
					isTogather : isTogather,
					price_l:param.price,
				    togatherPrice:param.togatherPrice,
				    nos : param.nos,
				    nos_checkout:param.nos_checkout,
					bz:el.find("#guest_remark").val()?el.find("#guest_remark").val():"",//备注
					method:"RM",//支付类型
					odata:json?json:{},//客账数据
					operation:param.operation,
					xhs:param.xhs,
					//初始化数据返回回调
					getDefaultBack:function(data){
						t.goon();
    					param.dataBack(type);
					},
					returnBack : function(){
						t.goon();
					}
				});	
    		});
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			var price = checkTwoPoint((parseFloat(param.price)+parseFloat(param.togatherPrice)));
    		param.totalPrice = price;
			this.el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
			var html = 	'<div class="mykb-box" id="mykeyboard"></div>'+
						'<div id="modal-dialog" class="modal-dialog" style="width:600px;">'+
							'<div id="pageOverlay" class="hide"></div>'+
							'<div id="loading" class="loading hide">'+
								'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
							'</div>'+
							'<div class="modal-content">'+
								'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
											'<span class="white">&times;</span>'+
										'</button>'+
										'<span id="title-dialog">'+$.i18n.propJsp(param.title)+'</span>'+
									'</div>'+
								'</div>'+
								'<div class="modal-body" style="min-height: 100px;">'+
									'<form id="frm" class="form-horizontal">'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("科目:")+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="profile-info-row">'+
													'<div class="profile-info-value" style="border:0;">'+
														'<span class="allprice" id="ar_subject"></span>'+
													'</div>'+
												'</div>'+
											'</div>'+
										'</div>'+
										'<div class="space-6"></div>'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("付款:")+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="input-group" style="width:245px;">'+
													'<input type="text" class="form-control search-query input_all" placeholder="" name="disrate" id="pay" value="'+ price +'">'+
													'<span class="input-group-btn">'+
														'<button type="button" class="btn btn-primary btn-sm" id="pay_btn">'+
															'<span class="ace-icon fa fa-keyboard-o icon-on-right bigger-110"></span>'+
														'</button>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
										'<div class="space-6"></div>'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("房号:")+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="input-group" style="width:245px;">'+
													'<input type="text" class="form-control search-query input_all" placeholder="" name="disrate" id="guest_barcode">'+
													'<span class="input-group-btn">'+
														'<button type="button" class="btn btn-primary btn-sm" id="guest_barcode_search">'+
															'<span class="ace-icon fa fa-search icon-on-right bigger-110"></span>'+
														'</button>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
										'<div class="space-6"></div>'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("姓名:")+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="input-group" style="width:245px;">'+
													'<input type="text" class="form-control search-query input_all" placeholder="" name="disrate" id="guest_user">'+
													'<span class="input-group-btn">'+
														'<button type="button" class="btn btn-primary btn-sm" id="guest_user_search">'+
															'<span class="ace-icon fa fa-search icon-on-right bigger-110"></span>'+
														'</button>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
										'<div class="space-6"></div>'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("备注:")+'</label>'+
											'<div class="col-sm-9">'+
												'<span class="input-icon input-icon-right">'+
													'<textarea class="form-control search-query" cols="50" rows="5" placeholder="" name="disrate" id="guest_remark"></textarea>'+
												'</span>'+
											'</div>'+
										'</div>'+
										'<div class="space-6"></div>'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("应付金额:")+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="profile-info-row">'+
													'<div class="profile-info-value" style="border:0;">'+
														'<span class="allprice" id="ar_subject" style="color:red !important;">'+ price +'</span>'+
													'</div>'+
												'</div>'+
											'</div>'+
										'</div>'+
									'</form>'+
								'</div>'+
								'<div class="modal-footer">'+
									'<div class="clearfix">'+
										'<div class="pull-right">'+
											'<div class="btn-group">'+
												'<button id="modalBtnSave" type="button" class="btn btn-sm btn-info">'+
													'<i class="ace-icon fa fa-check"></i>'+
													$.i18n.propJsp("保存")+
												'</button>'+
											'</div>'+
											'<div class="btn-group">'+
												'<button id="modalBtnCancel" type="button" class="btn btn-sm btn-default">'+
													'<i class="ace-icon fa fa-times"></i>'+
													$.i18n.propJsp("取消")+
												'</button>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			el.html(html);
    		var text = (param.bh||"") + "-" + (param.bhText||"");
    		if(text!="-"){
    			el.find("#ar_subject").text(text);
    		}
		},
		wait : function(){
			var el = this.el;
			el.find("#loading").removeClass("hide");
			el.find("#pageOverlay").removeClass("hide");
		},
		goon : function(){
			var el = this.el;
			el.find("#loading").addClass("hide");
			el.find("#pageOverlay").addClass("hide");
		},	
	}
	
	function checkTwoPoint(value){  		
	    value=value.toString()+".00";
	    value = value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
	    value = value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
	    value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
	    return value;
	}
})(jQuery);